<script setup>
import { Delete, Edit, Lock, Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref, reactive, onMounted } from 'vue';
import {del, get,post, put} from '../../api/http'

const apiList = reactive([])
const total = ref(0)
const roleList = reactive([])
const keyword = ref('')
const showApiDialog = ref(false);
const api = ref({})
const selectedRoleList = ref([])
const currentApi = reactive({})
const isEdit = ref(false)
const loading = ref(false)

const currentPageNum = ref(1)

const getRoleList = () => {
    get('role/list').then(res=>{
        roleList.value = res.data.list
    })
}

const getApiList = () => {
    loading.value = true
    get('apiPattern/list',{keyword:keyword.value,pageNum:currentPageNum.value,pageSize:10}).then(res=>{
        if(res.status){
            apiList.value = res.data.list
            total.value = res.data.total
        }else{
            ElMessage({type:'error',message:res.msg})
        }
        loading.value = false
    })
}

const handlePageChange = (pageNum) => {
    currentPageNum.value = pageNum
    getApiList()
}

const submitApi = () => {
    let roleList = []
    selectedRoleList.value.forEach(element => {
        roleList.push({id:element})
    });
    api.value.roleList = roleList;
    if(isEdit.value){
        put('apiPattern/set',api.value).then(res=>{
            ElMessage({type:res.status?'success':'error',message:res.msg})
            if(res.status){
                getApiList()
                showApiDialog.value = false;
            }
        })
    }else{
        post('apiPattern/add',api.value).then(res=>{
            ElMessage({type:res.status?'success':'error',message:res.msg})
            if(res.status){
                getApiList()
                showApiDialog.value = false;
            }
        })
    }
    
}

const removeApi = (id) => {
    ElMessageBox.confirm('该操作将永久删除该API，是否继续','提示',{type:'warning'}).then(()=>{
        if(id != -1){
            del('apiPattern/del',[id]).then(res=>{
                ElMessage({type:res.status?'success':'error',message:res.msg})
                if(res.status){
                    getApiList()
                }
            })
        }
    })
}

const editApi = (data) => {
    isEdit.value = true
    getRoleList()
    api.value = data;
    selectedRoleList.value = []
    data.roleList.forEach(element => {
        selectedRoleList.value.push(element.id)
    });
    showApiDialog.value = true;
    console.log('edit api ',api.value)
}

const resetForm = () => {
    console.log("rest api form")
    api.value = {}
    selectedRoleList.value = []
}

onMounted(()=>{
    getApiList()
})

</script>
<template>
    <main v-blog-title data-title="权限管理">
        <div class="toolBar">
            <el-input v-model="keyword" @input="getApiList" style="width: 300px; margin-right: 10px;" placeholder="关键字搜索"></el-input>
            <el-button @click="getRoleList();showApiDialog=true;isEdit=false;resetForm();" type="primary" :icon="Plus">添加API</el-button>
            <el-button type="danger" :icon="Delete">删除</el-button>
        </div>
        <div class="content" v-loading="loading">
            <el-table :data="apiList.value" border>
                <el-table-column label="API名称" prop="apiDescribe"></el-table-column>
                <el-table-column label="API URL">
                    <template #default="scope">
                        <code><span style="font-weight: bold;">{{ scope.row.apiPattern }}</span></code>
                    </template>
                </el-table-column>
                <el-table-column label="是否启用" align="center">
                    <template #default="scope">
                        <span style="font-size:14px;">{{ scope.row.enabled?'已启用':'未启用' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button size="small" plain @click="editApi(scope.row)" type="primary" :icon="Edit">编辑</el-button>
                        <el-button size="small" plain @click="removeApi(scope.row.id)" type="danger" :icon="Delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination small style="margin: 10px;" background @current-change="handlePageChange" layout="prev, pager, next" :total="total"></el-pagination>
        </div>
        <el-dialog title="API信息" v-model="showApiDialog">
            <el-form label-width="80">
                <el-form-item label="API名称">
                    <el-input v-model="api.apiDescribe" placeholder="输入名称"></el-input>
                </el-form-item>
                <el-form-item label="API URL">
                    <el-input v-model="api.apiPattern" placeholder="输入URL"></el-input>
                </el-form-item>
                <el-form-item label="对应角色">
                    <el-select style="width:100%" v-model="selectedRoleList" placeholder="选择角色" multiple>
                        <el-option v-for="item in roleList.value" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-switch v-model="api.enabled"></el-switch>
                </el-form-item>
                <el-form-item>
                    <div style="width: 100%; text-align: right;">
                        <el-button type="info" @click="showApiDialog=false">取消</el-button>
                        <el-button @click="submitApi" type="primary">确定</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </main>
</template>
<style scoped>
.toolBar{
    padding: 10px;
}
.content{
    margin: 10px;
}
</style>